﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SenerPro;
using System.IO;
using System.Text;

namespace MensUnoShop.Models
{
    public class PayFactory: ITranscation
    {
        private static Logger _log = new Logger(Config.logpath);
        protected static DBPay _dbp = new DBPay();

        public int? GetCashid(string txid, int? amount, int? paytype)
        {
            return _dbp.GetCashid(txid, amount, paytype);
        }

        public string DoCreditcardTrade(string txid, int amount, ref string cashid)
        {
            PayInfo pi = new PayInfo();
            pi.CASHID = GetCashid(txid, amount, (int)PayType.CreditCard);
            pi.TXID = txid;
            pi.PAYTYPE = PayType.CreditCard;
            pi.AMOUNT = amount;
            pi.PICCARD.PERIOD = 1;
            pi.PICCARD.APPROVEFLAG = "1";
            pi.PICCARD.DEPOSITFLAG = "0";
            pi.PICCARD.ENGLISH = "0";
            pi.PICCARD.CHECKSUM1 = GetMD5Hash(Config.MerchantNumber1 + pi.CASHID + Config.R_Code1 + pi.AMOUNT);

            UptCashOrder(pi);
            AddCreditcardData(pi.CASHID, pi.PICCARD.PERIOD, pi.PICCARD.CHECKSUM1, pi.PICCARD.CHECKSUM2, pi.PICCARD.CHECKSUM3, pi.PICCARD.PRC, pi.PICCARD.SRC,
                pi.PICCARD.APPROVALCODE, pi.PICCARD.BANKRESPONSECODE, pi.PICCARD.BATCHNUMBER, pi.PICCARD.CARDNUMBER, pi.PICCARD.ISSUEBANK, pi.PICCARD.ENGNAME);

            StringBuilder sb1 = new StringBuilder(3000);
            sb1 = sb1.Append("<form name='payInfoForm' action='" + Config.PayLink1 + "' method='post'>");
            sb1 = sb1.Append("<input type='hidden' name='MerchantNumber' value='" + Config.MerchantNumber1 + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='OrderNumber' value='" + pi.CASHID + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='Amount' value='" + pi.AMOUNT + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='OrgOrderNumber' value='" + pi.TXID + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='ApproveFlag' value='" + pi.PICCARD.APPROVEFLAG + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='DepositFlag' value='" + pi.PICCARD.DEPOSITFLAG + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='Englishmode' value='" + pi.PICCARD.ENGLISH + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='OrderURL' value='" + Config.OrderURL1 + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='ReturnURL' value='" + Config.ReturnURL1 + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='checksum' value='" + pi.PICCARD.CHECKSUM1 + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='op' value='AcceptPayment'/>");
            sb1 = sb1.Append("<input type='text' name='ff'/>");
            sb1 = sb1.Append("<input type='submit' value='如頁面無自動轉頁，請按下此按鈕!' /></form>\n");
            sb1 = sb1.Append("<script language='javascript'>document.payInfoForm.submit();</script>");
            cashid = pi.CASHID.ToString();
            return sb1.ToString();
        }

        public string DoCSTrade(string txid, int amount, string paytitle, DateTime duedate, string payname, string payphone, PayType pt, ref string cashid)
        {
            PayInfo pi = new PayInfo();
            pi.CASHID = GetCashid(txid, amount, (int)PayType.CS);
            pi.TXID = txid;
            pi.PAYTYPE = pt;
            pi.AMOUNT = amount;

            pi.PICS.PAYTITLE = paytitle;
            if (pt == PayType.ATM || pt == PayType.WebATM)
                pi.PICS.BANKID = "004";
            else
                pi.PICS.BANKID = string.Empty;
            
            pi.PICS.DUEDATE = int.Parse(duedate.ToString("yyyyMMdd"));
            pi.PICS.PAYNAME = payname;
            pi.PICS.PAYPHONE = payphone;
            pi.PICS.RETURNVALUE = 0;
            pi.PICS.HASH = GetMD5Hash(Config.MerchantNumber2 + Config.Code2 + pi.AMOUNT + pi.CASHID);
            pi.PICS.NEXTURL = Config.Site;

            UptCashOrder(pi);
            AddUptCSDataData(pi.CASHID, pi.PICS.PAYTITLE, duedate, pi.PICS.PAYNAME, pi.PICS.PAYPHONE, pi.PICS.HASH);

            StringBuilder sb1 = new StringBuilder(3000);
            sb1 = sb1.Append("<form name='payInfoForm' action='" + Config.PayLink2 + "' method='post'>");
            sb1 = sb1.Append("<input type='hidden' name='MerchantNumber' value='" + Config.MerchantNumber2 + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='OrderNumber' value='" + pi.CASHID + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='Amount' value='" + pi.AMOUNT + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='paymenttype' value='" + pi.PAYTYPE.ToString().ToUpper() + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='bankid' value='" + pi.PICS.BANKID + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='paytitle' value='" + pi.PICS.PAYTITLE + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='paymemo' value=''/>");
            sb1 = sb1.Append("<input type='hidden' name='payname' value='" + pi.PICS.PAYNAME + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='payphone' value='" + pi.PICS.PAYPHONE + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='duedate' value='" + pi.PICS.DUEDATE + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='returnvalue' value='" + pi.PICS.RETURNVALUE + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='hash' value='" + pi.PICS.HASH + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='nexturl' value='" + pi.PICS.NEXTURL + "'/>");
            sb1 = sb1.Append("<input type='text' name='ff'/>");
            sb1 = sb1.Append("<input type='submit' value='如頁面無自動轉頁，請按下此按鈕!' /></form>\n");
            sb1 = sb1.Append("<script language='javascript'>document.payInfoForm.submit();</script>");
            cashid = pi.CASHID.ToString();
            return sb1.ToString();
        }

        public string DoCSTradeQuery(int cashid)
        {
            PayInfo pi = new PayInfo();
            PayInfoCS pics = new PayInfoCS();
            pi = GetPayInfo(cashid);
            pics = GetPayInfoCS(cashid);
            pics.RETURNVALUE = 0;
            pics.NEXTURL = Config.Site;
            if (pi.PAYTYPE == PayType.ATM || pi.PAYTYPE == PayType.WebATM)
            {
                pics.BANKID = "004";
            }
            pi.PICS = pics;

            StringBuilder sb1 = new StringBuilder(3000);
            sb1 = sb1.Append("<form name='payInfoForm' action='" + Config.QueryLink2 + "' method='post'>");
            sb1 = sb1.Append("<input type='hidden' name='MerchantNumber' value='" + Config.MerchantNumber2 + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='OrderNumber' value='" + pi.CASHID + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='Amount' value='" + pi.AMOUNT + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='paymenttype' value='" + pi.PAYTYPE.ToString().ToUpper() + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='bankid' value='" + pi.PICS.BANKID + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='paytitle' value='" + pi.PICS.PAYTITLE + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='paymemo' value=''/>");
            sb1 = sb1.Append("<input type='hidden' name='payname' value='" + pi.PICS.PAYNAME + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='payphone' value='" + pi.PICS.PAYPHONE + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='duedate' value='" + pi.PICS.DUEDATE + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='returnvalue' value='" + pi.PICS.RETURNVALUE + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='hash' value='" + pi.PICS.HASH + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='nexturl' value='" + pi.PICS.NEXTURL + "'/>");
            sb1 = sb1.Append("<input type='hidden' name='operation' value='regetorder'/>");
            sb1 = sb1.Append("<input type='text' name='ff'/>");
            sb1 = sb1.Append("<input type='submit' value='如頁面無自動轉頁，請按下此按鈕!' /></form>\n");
            sb1 = sb1.Append("<script language='javascript'>document.payInfoForm.submit();</script>");

            return sb1.ToString();
        }
        
                

        public PayInfo GetPayInfo(int cashid)
        {
            return _dbp.GetPayInfo(cashid);
        }

        public PayInfoCreditcard GetPayInfoCreditcard(int cashid)
        {
            return _dbp.GetPayInfoCreditcard(cashid);
        }

        public PayInfoCS GetPayInfoCS(int cashid)
        {
            return _dbp.GetPayInfoCS(cashid);
        }

        public string GetMD5Hash(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            string password = s.ToString();
            return password;
        }
        


        public void UptCashOrder(PayInfo pi)
        {
            _dbp.UptCashOrder(pi.CASHID, pi.TXID, pi.AMOUNT, pi.PAYTYPE);
        }

        public void AddCreditcardData(int? cashid, int period, string checksum1, string checksum2, string checksum3, string prc, string src,
            string approvalcode, string bankresponsecode, string batchnumber, string cardnumber, string cardissuebank, string engname)
        {
            _dbp.AddCreditcardData(cashid, period, checksum1, checksum2, checksum3, prc, src,
                approvalcode, bankresponsecode, batchnumber, cardnumber, cardissuebank, engname);
        }

        public void UptCreditcardData(int? cashid, int period, string checksum1, string checksum2, string checksum3, string prc, string src,
            string approvalcode, string bankresponsecode, string batchnumber, string cardnumber, string cardissuebank, string engname)
        {
            _dbp.UptCreditcardData(cashid, period, checksum1, checksum2, checksum3, prc, src,
                approvalcode, bankresponsecode, batchnumber, cardnumber, cardissuebank, engname);
        }

        public void AddUptCSDataData(int? cashid, string paytitle, DateTime duedate, string payname, string payphone, string hash)
        {
            _dbp.AddUptCSDataData(cashid, paytitle, duedate, payname, payphone, hash);
        }

        public void UptCSData(int? cashid, string paytitle, DateTime duedate, string payname, string payphone, string hash)
        {
            _dbp.UptCSData(cashid, paytitle, duedate, payname, payphone, hash);
        }

        public void UptCoLog(int? cashid, string newstatus)
        {
            _dbp.UptCoLog(cashid, newstatus);
        }
    }
}